package com.rongzhe.house.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.ui.activity.RentTypeActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huang on 2017/12/2.
 */

public class HouseAdapter2 extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<HouseOverview> mRentAllList = new ArrayList<>();
    private List<HouseOverview> mRentApartmentList = new ArrayList<>();
    private List<HouseOverview> mRentShareList = new ArrayList<>();
    private List<HouseOverview> mRentDirectList = new ArrayList<>();
    private List<HouseOverview> mRentRecommend = new ArrayList<>();


    public HouseAdapter2(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
    }

    public void updateUi() {
        notifyDataSetChanged();
    }

    public void setRentAllList(List<HouseOverview> mRentAllList) {
        this.mRentAllList = mRentAllList;
    }

    public void setRentApartmentList(List<HouseOverview> mRentApartmentList) {
        this.mRentApartmentList = mRentApartmentList;
    }

    public void setRentShareList(List<HouseOverview> mRentShareList) {
        this.mRentShareList = mRentShareList;
    }

    public void setRentDirectList(List<HouseOverview> mRentDirectList) {
        this.mRentDirectList = mRentDirectList;
    }

    public void setRentRecommend(List<HouseOverview> mRentRecommend) {
        this.mRentRecommend = mRentRecommend;
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View root = mLayoutInflater.inflate(R.layout.list_item_main_page, parent,false);
        RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.list_house);
        TextView titleText = (TextView) root.findViewById(R.id.text_title);
        TextView findMoreBtn = (TextView) root.findViewById(R.id.btn_find_more);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        MainRentTypeAdapter adapter = new MainRentTypeAdapter(mContext);
        recyclerView.setAdapter(adapter);
        List<HouseOverview> data = null;
        String type = null;
        switch (position) {
            case 0:
                titleText.setText(R.string.rent_apartment);
                data = mRentApartmentList;
                type = mContext.getString(R.string.rent_apartment);
                break;
            case 1:
                titleText.setText(R.string.rent_together);
                data = mRentShareList;
                type = mContext.getString(R.string.rent_together);
                break;
            case 2:
                titleText.setText(R.string.rent_all);
                data = mRentAllList;
                type = mContext.getString(R.string.rent_all);
                break;
            case 3:
                titleText.setText(R.string.rent_direct);
                data = mRentDirectList;
                type = mContext.getString(R.string.rent_direct);
                break;
            case 4:
                titleText.setText(R.string.rent_recomment);
                data = mRentRecommend;
                type = mContext.getString(R.string.rent_recomment);
                break;
        }
        findMoreBtn.setTag(type);
        findMoreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchTypeActivity((String) v.getTag());
            }
        });
        adapter.setDatas(data);
        return root;
    }

    private void launchTypeActivity(String type) {
        Intent intent = new Intent(mContext, RentTypeActivity.class);
        intent.putExtra(RentTypeActivity.EXTRA_TITLE, type /**/);
        mContext.startActivity(intent);
    }

}
